<template>
  <el-dialog
    v-model="dialogTableVisible"
    :title="$t('groups.groupOrderList')"
    width="60%"
    :close-on-click-modal="false"
  >
    <el-table
      :data="tableData"
      header-cell-class-name="table-header"
      row-class-name="table-row-low"
      style="width: 100%"
    >
      <el-table-column
        prop="orderNumber"
        :label="$t('order.number')"
        width="300"
      >
        <template #default="scope">
          {{ scope.row.orderNumber || '-' }}
        </template>
      </el-table-column>
      <el-table-column
        prop="activityProdPrice"
        :label="$t('group.salesPrice')"
        width="110"
      >
        <template #default="scope">
          {{ scope.row.activityProdPrice || '-' }}
        </template>
      </el-table-column>
      <el-table-column
        prop="groupPrice"
        :label="$t('groups.groupPrice')"
      >
        <template #default="scope">
          {{ scope.row.groupPrice || '-' }}
        </template>
      </el-table-column>
      <el-table-column
        prop="payPrice"
        :label="$t('order.actualAmount')"
      >
        <template #default="scope">
          {{ scope.row.payPrice || '-' }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('groups.membership')">
        <template #default="scope">
          <span v-if="scope.row.identityType === 1 && scope.row.userId !== 0">{{
            $t('groups.head')
          }}</span>
          <span v-if="scope.row.identityType === 0 && scope.row.userId !== 0">{{
            $t('groups.member')
          }}</span>
          <span v-if="scope.row.userId === '0'">{{ $t('groups.robot') }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        width="180"
        :label="$t('order.createTime')"
      />
      <el-table-column
        :label="$t('text.menu')"
        align="center"
        width="180"
      >
        <template #default="scope">
          <el-button
            type="text"
            :disabled="scope.row.userId === '0' || !scope.row.userId"
            @click="viewOrder(scope.row.orderNumber)"
          >
            {{ $t('order.viewOrder') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-if="tableData.length"
      :current-page="page.currentPage"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="page.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.total"
      @update:page-size="onPageSizeChange"
      @update:current-page="onPageChange"
    />
  </el-dialog>
</template>

<script setup>
const router = useRouter()

const tableData = ref([])
const page = reactive({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})
const dialogTableVisible = ref(false)
const GroupOrder = {
  groupTeamId: null
}

const init = groupTeamId => {
  dialogTableVisible.value = true
  nextTick(() => {
    if (groupTeamId) {
      GroupOrder.groupTeamId = groupTeamId
      getDataList(page)
    }
  })
}
const getDataList = pageParam => {
  http({
    url: http.adornUrl('/group/team/info'),
    method: 'get',
    params: http.adornParams(
      Object.assign(
        {
          current: pageParam == null ? page.currentPage : pageParam.currentPage,
          size: pageParam == null ? page.pageSize : pageParam.pageSize
        },
        GroupOrder
      )
    )
  }).then(({ data }) => {
    tableData.value = data.records
    page.total = data.total
  })
}
const viewOrder = (orderNumber, userId) => {
  if (Number(userId) === 0) {
    return
  }
  dialogTableVisible.value = false
  // TODO: 跳转到订单详情
  router.push({
    path: '/order/order-manage/order/index',
    query: { orderNumber }
  })
}
const onPageSizeChange = val => {
  page.pageSize = val
  getDataList()
}
const onPageChange = val => {
  page.currentPage = val
  getDataList()
}

// expose public methods
defineExpose({
  init
})
</script>
